﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BarcodeDelay.business
{
    public class FileWatchManager
    {
        private static FileWatchManager instance;
        public static FileWatchManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FileWatchManager();
                }
                return instance;
            }
        }

        private Thread fThreadDep = null;
        private Thread fThreadArr = null;
        private Thread fThreadFruit = null;
        private ArrayList m_arListDepBarcodes = new ArrayList();
        int barCodeNum = 0;
        int curBarcodeIndex = 1;
        DateTime dtLastChange = System.DateTime.Now;
        public void BeginAnalyThread(ArrayList arListBarCodes, string anayType)
        {
            try
            {
                curBarcodeIndex = 1;
                if (fThreadDep != null && (fThreadDep.ThreadState == ThreadState.Running || fThreadDep.ThreadState == ThreadState.Background))
                {
                    fThreadDep.Abort();
                    System.Threading.Thread.Sleep(50);
                }
                if (fThreadDep != null && (fThreadDep.ThreadState == ThreadState.Running || fThreadDep.ThreadState == ThreadState.Background))
                {
                    while (fThreadDep.ThreadState != ThreadState.Aborted)
                    {
                        //当调用Abort方法后，如果thread线程的状态不为Aborted，主线程就一直在这里做循环，直到thread线程的状态变为Aborted为止
                        Thread.Sleep(100);
                    }
                }


                m_arListDepBarcodes = arListBarCodes;
                fThreadDep = new Thread(new ThreadStart(parseDepartureBarcodes));//出港分析 开辟一个新的线程
                fThreadDep.IsBackground = true;
                fThreadDep.Start();


            }
            catch (Exception ex)
            {

            }
        }

        private void parseDepartureBarcodes()
        {
            try
            {
                string strDir = StoParams.Instance.roadFilePath.Substring(0, StoParams.Instance.roadFilePath.LastIndexOf('\\'));
                string filrName = StoParams.Instance.roadFilePath.Substring(StoParams.Instance.roadFilePath.LastIndexOf('\\') + 1);
                WatcherStrat(strDir, filrName);
            }
            catch(Exception ex)
            {

            }

        }
        private  void WatcherStrat(string path, string filter)
        {
            try
            {
                FileSystemWatcher watcher = new FileSystemWatcher();
                watcher.Path = path;
                watcher.Filter = filter;
                watcher.Changed += new FileSystemEventHandler(OnProcess);
                watcher.Created += new FileSystemEventHandler(OnProcess);
                watcher.Deleted += new FileSystemEventHandler(OnProcess);
                watcher.Renamed += new RenamedEventHandler(OnRenamed);
                watcher.EnableRaisingEvents = true;
                watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess
                                       | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
                watcher.IncludeSubdirectories = true;
            }
            catch(Exception ex)
            {

            }
            
        }
        private  void OnProcess(object source, FileSystemEventArgs e)
        {
            if (e.ChangeType == WatcherChangeTypes.Created)
            {
                OnCreated(source, e);
            }
            else if (e.ChangeType == WatcherChangeTypes.Changed)
            {
                OnChanged(source, e);
            }
            else if (e.ChangeType == WatcherChangeTypes.Deleted)
            {
                OnDeleted(source, e);
            }

        }
        private  void OnCreated(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("文件新建事件处理逻辑 {0}  {1}  {2}", e.ChangeType, e.FullPath, e.Name);
        }
        private  void OnChanged(object source, FileSystemEventArgs e)
        {
            DateTime dtNow = System.DateTime.Now;
            TimeSpan ts = dtNow - dtLastChange;
            if(ts.TotalSeconds<4)
            {
                return;
            }
            Console.WriteLine("文件改变事件处理逻辑{0}  {1}  {2}", e.ChangeType, e.FullPath, e.Name);
            if(curBarcodeIndex>m_arListDepBarcodes.Count)
            {
                return;
            }
            dtLastChange = dtNow;
            ArrayList arList = new ArrayList();
            int beginindex = curBarcodeIndex;
            int endIndex = curBarcodeIndex;
            if (curBarcodeIndex< m_arListDepBarcodes.Count-1)
            {
                for (int index = curBarcodeIndex; index < beginindex + StoParams.Instance.queryCount; index++)
                {
                    if(index>m_arListDepBarcodes.Count-1)
                    {
                        break;
                    }
                    arList.Add(m_arListDepBarcodes[index]);
                    curBarcodeIndex = index;
                    endIndex++;
                }
                
                utility.Utility.addBarcodeToFile(arList, beginindex, endIndex, StoParams.Instance.fileDir);
            }
            else
            {
                // 发送给F12结束
            }
           
        }

        private  void OnDeleted(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("文件删除事件处理逻辑{0}  {1}   {2}", e.ChangeType, e.FullPath, e.Name);
        }

        private  void OnRenamed(object source, RenamedEventArgs e)
        {
            Console.WriteLine("文件重命名事件处理逻辑{0}  {1}  {2}", e.ChangeType, e.FullPath, e.Name);
        }


    }
}
